package makabakaquestionnaire.zutsoftware.service.impl;


import makabakaquestionnaire.zutsoftware.entity.Users;
import makabakaquestionnaire.zutsoftware.mapper.UsersMapper;
import makabakaquestionnaire.zutsoftware.service.MyUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.util.List;
/**
 * 弃用 目前使用 AuthLoginService做登陆认证
 */
//@Service
public class UserDetailsServiceImpl  implements UserDetailsService {

   private UsersMapper userMapper;
    @Autowired
    public void setUserMapper(UsersMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Users users;
        if(username!=null&&!"".equals(username)){
          users =userMapper.queryByName(username);
        }else {
            users=null;
        }

        if (users==null){
            throw new UsernameNotFoundException("用户名不存在");
        }else {
            List<GrantedAuthority>authorities= AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_admin");
            System.out.println(users.getUserName()+"登陆"+users.getPassword());
            return new MyUserDetails(authorities,new BCryptPasswordEncoder().encode(users.getPassword()),users.getUserName(),users.getId());
        }

    }
}
